<?php
namespace Admin\Controller;

use Admin\Controller\GlobalController;

class CommentController extends GlobalController
{
    public function getlist()
    {
        $pageSize = 20;
        $type = I('get.type', 1, 'intval');
        $where = array('is_deleted' => '0', 'type' => $type);
        $user_id = I('get.user_id', 0, 'intval');
        if (!empty($user_id)) {
            $where['user_id'] = $user_id;
        }
        $this->assign('user_id', $user_id == 0 ? '' : $user_id);
        $id = I('get.id', 0, 'intval');
        if (!empty($id)) {
            $where['id'] = $id;
        }
        $this->assign('id', $id == 0 ? '' : $id);
        $commentModel = M('comment');
        $count = $commentModel->where($where)->count();
        $pageObj = new \Admin\Util\Page($count, $pageSize);
        $list = $commentModel->where($where)->order('comment_id desc')->limit($pageObj->firstRow . ',' . $pageObj->listRows)->select();
        $userModel = M('user');
        $eggModel = M('egg');
        $newsModel = M('news');
        $spoilerModel = M('spoiler');
        foreach ($list as $key => $row) {
            $userInfo = $userModel->where(array('user_id' => $row['user_id']))->find();
            $list[$key]['username'] = $userInfo['username'];
            $list[$key]['nickname'] = $userInfo['nickname'];

            $list[$key]['remark'] = '';
            if ($row['type'] == 1) {
                $eggInfo = $eggModel->where(array('egg_id' => $row['id']))->find();
                $list[$key]['remark'] = "[{$eggInfo['row_number']}] {$eggInfo['egg_name']}";
            } elseif ($row['type'] == 2) {
                $newsInfo = $newsModel->where(array('news_id' => $row['id']))->find();
                $list[$key]['remark'] = "[{$newsInfo['news_id']}] {$newsInfo['news_title']}";
            } elseif ($row['type'] == 3) {
                $spoilerInfo = $spoilerModel->where(array('spoiler_id' => $row['id']))->find();
                $list[$key]['remark'] = "[{$spoilerInfo['spoiler_id']}] {$spoilerInfo['spoiler_title']}";
            }
        }
        $this->assign('type', $type);
        $type_list = array('1' => '彩蛋', '2' => '资讯', '3' => '剧透',);
        $this->assign('type_list', $type_list);
        $this->assign('list', $list);
        $this->assign('page', $pageObj->show());
        $this->display();
    }

    //添加或者修改
    public function del()
    {
        $comment_id = I('post.comment_id', 0, 'trim');
        $comment_ids = explode(',', $comment_id);
        $commentModel = M('comment');
        $commentModel->where(array('comment_id' => array('IN', $comment_ids)))->setField('is_deleted', '1');
        $this->ajaxReturn('', '操作成功', 'TIP_SUCCESS');
    }
}